package com.kaisheng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaisheng.entity.MyFile;
import com.kaisheng.entity.Files;
import com.kaisheng.entity.User;
import com.kaisheng.util.DBHelp;
import com.kaisheng.util.RowMapper;

public class FileDao {
	DBHelp<MyFile> db = new DBHelp<MyFile>();
	public List<MyFile> findByFilesId(String filesId) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t_file.id AS file_id,t_file.filename AS file_filename,t_file.filesize AS file_filesize,t_file.user_id AS file_userid,t_file.name AS file_name,t_file.createtime AS file_createtime,");
		sql.append("t_file.files_id AS file_filesid,t_user.id AS user_id,t_user.name AS user_name,t_files.name AS files_name ");
		sql.append("FROM t_file LEFT JOIN t_user ON t_file.user_id = t_user.id ");
		sql.append("LEFT JOIN t_files ON t_file.files_id = t_files.id ");
		sql.append("WHERE t_file.files_id = ?");
		return db.executeQueryForList(sql.toString(), new FileRowMapper(), filesId);
		
	}
	
	public MyFile findByFilename(String filename) {
		String sql = "select id,filename,filesize,files_id,user_id,`name`,createtime from t_file where filename = ?";
		return db.executeQueryForObject(sql, FileA, filename);
	}


	public boolean save(MyFile myfile) {
		String sql = "INSERT INTO t_file(id,filename,filesize,files_id,user_id,`name`,createtime) VALUES (?,?,?,?,?,?,?)";
		return db.executeSQL(sql, myfile.getId(),myfile.getFilename(),myfile.getFilesize(),myfile.getFiles_id(),myfile.getUser_id(),myfile.getName(),myfile.getCreatetime());
	}
	
	public boolean del(String filename) {
		String sql = "DELETE FROM  t_file WHERE filename = ?";
		return db.executeSQL(sql, filename);
	} 

	
	private class FileRowMapper implements RowMapper<MyFile>{

		
		
		public MyFile mapRow(ResultSet rs) throws SQLException {
			MyFile file = new MyFile();
			file.setId(rs.getString("file_id"));
			file.setFilename(rs.getString("file_filename"));
			file.setFilesize(rs.getString("file_filesize"));
			file.setUser_id(rs.getString("file_userid"));
			file.setName(rs.getString("file_name"));
			file.setCreatetime(rs.getString("file_createtime"));
			file.setFiles_id(rs.getString("file_filesid"));
			
			User user = new User();
			user.setId(rs.getString("user_id"));
			user.setName(rs.getString("user_name"));
			
			file.setUser(user);
			
			Files files = new Files();
			files.setName(rs.getString("files_name"));
			
			file.setFiles(files);
			
			return file;
		}
	}
	RowMapper<MyFile> FileA = new RowMapper<MyFile>(){

		public MyFile mapRow(ResultSet rs) throws SQLException {
			MyFile file = new MyFile();
			file.setId(rs.getString("id"));
			file.setFilename(rs.getString("filename"));
			file.setFilesize(rs.getString("filesize"));
			file.setUser_id(rs.getString("user_id"));
			file.setName(rs.getString("name"));
			file.setCreatetime(rs.getString("createtime"));
			file.setFiles_id(rs.getString("files_id"));
			
			return file;
		}
		
		
		
	};
	

	
}
